<?php
class Core_Data_Mappers_Resource extends Core_Data_Mappers_Abstract {
	protected $_modelclass="Core_Data_Models_Resource";
	protected $_businessclass="Core_Authentication_Business_Resource";
	protected $_map=array("Id"=>"id","Name"=>"name","Controller"=>"controllername","Created"=>"created","Updated"=>"updated");
	
	public function find($id) {
		$row = $this->getDbTable()->fetchRow(array("id = ?"=>$id));
		if(isset($row) && count($row)>0) {
			return $this->map($row);
		}
		return null;
	}
	
	public function exists($object) {
		$o = $this->find($object->Id);
		if(isset($o) && $o instanceof Core_Authentication_Business_Resource) {
			return true;
		}
		else {
			return false;
		}
		
	}
	public function add($object) {
		if(!$this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['id']);
			$id = $this->getDbTable()->insert($data);
			$object->Id = $id;
		}
	}
	public function edit($object) {
		if($this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['id']);
			$this->getDbTable()->update($data, array("id = ?"=>$object->Id));
		}
		
	}
	public function fetchAll($where=null,$count=10,$start=0) {
		$rows = $this->getDbTable()->fetchAll($where,null,$count,$start);
		$collection = array();
		if(isset($rows) && count($rows)>0) {
			foreach ($rows as $item) {
				$collection[] = $this->map($item);
			}
		}
		return $collection;
	}
	public static function getById ($id) {
		$me = new Core_Data_Mappers_Resource();
		return $me->find($id);
	}
}
?>